package com.isesol.ime.service;

import com.google.common.base.*;
import com.google.common.collect.*;
import com.isesol.ime.*;
import com.isesol.ime.dal.mapper.*;
import com.isesol.ime.dal.model.*;
import com.isesol.ime.dal.model.dto.*;
import com.isesol.ime.entity.*;
import lombok.extern.slf4j.*;
import org.apache.commons.collections.*;
import org.apache.commons.collections.map.*;
import org.apache.commons.lang3.*;
import org.springframework.stereotype.*;

import javax.annotation.*;
import java.util.*;

/**
 * 下载
 * @author peter zhang
 */
@Component
@Slf4j
public class DownloadService{

	@Resource
	SubscriptionMapper subscriptionMapper;

	@Resource
	MessageMapper messageMapper;

	public Map getData(long subId){

		Map eventMap = Maps.newHashMap();

		SubscriptionDO subscriptionDO = subscriptionMapper.getById(subId);
		MessageDO      messageDO      = messageMapper.getById(subscriptionDO.getMsgId());

		List<SubscriptionDetailDTO> subscriptionDetailDTOList = subscriptionMapper.findDetail(
				subscriptionDO.getMachineId(), messageDO.getRuleName());

 		if(CollectionUtils.isNotEmpty(subscriptionDetailDTOList)){
			SubscriptionDetailDTO subscriptionDetailDTO = subscriptionDetailDTOList.get(0);
			// 指令
			if (Constant.RULE_NAME_TIME.equals(subscriptionDetailDTO.getRuleName())){
				eventMap.put("subData", getSubData(subscriptionDetailDTOList));
				eventMap.put("fileName", "subscrData.json");
			}if (Constant.RULE_NAME_EVENT.equals(subscriptionDetailDTO.getRuleName())){
				// 事件
				eventMap.put("eventData", getEvents(subscriptionDetailDTOList));
				eventMap.put("fileName", "eventData.json");
			}
		}else{
			if (Constant.RULE_NAME_TIME.equals(messageDO.getRuleName())){
				eventMap.put("subData", "");
				eventMap.put("fileName", "subscrData.json");
			} else if (Constant.RULE_NAME_EVENT.equals(messageDO.getRuleName())){
				// 事件
				eventMap.put("eventData", "");
				eventMap.put("fileName", "eventData.json");
			}
		}

		return eventMap;
	}

	private List<Event> getEvents(List<SubscriptionDetailDTO> messages){

		List<Event> eventList = Lists.newArrayList();

		Map<String, SubscriptionDetailDTO> m = new MultiValueMap();

		for (SubscriptionDetailDTO message : messages){
			m.put(message.getMsgNumber(), message);
		}
		for (String key : m.keySet()){
			List<SubscriptionDetailDTO> list = (List) m.get(key);
			SubscriptionDetailDTO oneSubscriptionDetailDTO=list.get(0);
			Event event = new Event();
			event.setEventName(StringUtils.strip(oneSubscriptionDetailDTO.getRule()));
			event.setOrder(key);
			event.setReply(oneSubscriptionDetailDTO.isFeedback() ? "1" : "0");
			event.setStore(oneSubscriptionDetailDTO.isPreservation() ? "1" : "0");
			event.setEncode(oneSubscriptionDetailDTO.isEncrypt() ? "1" : "0");

			List<String> paramNameList = Lists.newArrayList();
			for (SubscriptionDetailDTO subscriptionDetailDTO : list){
				paramNameList.add(subscriptionDetailDTO.getName());
			}
			event.setEventContent(Joiner.on("|").join(paramNameList));
			eventList.add(event);

		}
			return eventList;
	}

	private List<Data> getSubData(List<SubscriptionDetailDTO> messages){

		Map<String, SubscriptionDetailDTO> m = new MultiValueMap();

		List<Data>   subdataList   = Lists.newArrayList();

		for (SubscriptionDetailDTO message : messages){
			m.put(message.getMsgNumber(), message);
		}

		for (String key : m.keySet()){
			List<SubscriptionDetailDTO> list = (List) m.get(key);
			Data data = new Data();
			List<String> paramNameList = Lists.newArrayList();
			for (SubscriptionDetailDTO dto : list){
				data.setType(dto.getMsgNumber());
				paramNameList.add(dto.getName());
			}
			data.setSubContent(Joiner.on("|").join(paramNameList));

			subdataList.add(data);
		}

		return subdataList;

	}

}
